package com.wt.struggle.goodbye.message

import android.app.Activity.RESULT_OK
import android.content.Intent
import android.os.Bundle
import android.support.design.widget.TabLayout
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.google.gson.reflect.TypeToken
import com.wt.struggle.goodbye.MainActivity
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.*
import com.wt.struggle.goodbye.info.MessageInfo
import com.wt.struggle.goodbye.info.NetInfo
import kotlinx.android.synthetic.main.message_fragment_layout.*

class MessageFragment : ProV4Fragment() {

    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {

        when (what) {

            Send.GET_MESSAGE_CODE -> {

                messageRefreshView.isRefreshing = false
                removeLoadDialog()

                if (netInfo.code == Config.SUCCESS) {

                    val result = netInfo.dataResult

                    if (result != null && result != "" && result != "null") {
                        val arr: ArrayList<MessageInfo> =
                            gson!!.fromJson(result, object : TypeToken<ArrayList<MessageInfo>>() {}.type)

                        if (arr.size != 0) {
                            show(arr)
                        } else {
                            if (isVisibleCode) {
                                showToastShort(activity!!, "无消息记录")
                            }

                        }

                    } else {
                        if (isVisibleCode) {
                            showToastShort(activity!!, "无消息记录")
                        }
                    }
                } else {
                    if (isVisibleCode) {
                        showToastShort(activity!!, "无消息记录")
                    }
                }
            }
        }
    }

    val messageList = arrayListOf<MessageInfo>()
    val siXinList = arrayListOf<MessageInfo>()

    fun show(arr: ArrayList<MessageInfo>) {
        for (temp in arr) {
            if (temp.type == 1) {
                messageList.add(temp)
            } else if (temp.type == 2) {
                siXinList.add(temp)
            }
        }

        change(indexTabP)
    }


    private fun change(i: Int) {
        val view = messageTabLayout.getTabAt(0)!!.customView
        val textNum = view!!.findViewById<TextView>(R.id.tvTabItemNum)
        val text1 = view.findViewById<TextView>(R.id.tvTabItemTitle)


        val view1 = messageTabLayout.getTabAt(1)!!.customView
        val textNum1 = view1!!.findViewById<TextView>(R.id.tvTabItemNum)
        val text2 = view1.findViewById<TextView>(R.id.tvTabItemTitle)

        val mmNum = getNoReadSize(messageList)

        if (mmNum != 0) {
            textNum.visibility = View.VISIBLE
            textNum.text = mmNum.toString()
        } else {
            textNum.visibility = View.GONE
        }

        val ssNum = getNoReadSize(siXinList)

        if (ssNum != 0) {
            textNum1.visibility = View.VISIBLE
            textNum1.text = ssNum.toString()
        } else {
            textNum1.visibility = View.GONE
        }

        (activity as MainActivity).showNum(mmNum + ssNum)


        when (i) {
            0 -> {
                text1.setTextColor(resources.getColor(R.color.red))
                text2.setTextColor(resources.getColor(R.color.black))
                adapter.updateDataClear(messageList)
            }

            1 -> {
                text1.setTextColor(resources.getColor(R.color.black))
                text2.setTextColor(resources.getColor(R.color.red))
                adapter.updateDataClear(siXinList)
            }
        }

        this.indexTabP = i

    }

    private fun getNoReadSize(messageList: ArrayList<MessageInfo>): Int {
        var num = 0

        for (temp in messageList) {
            if (temp.status == 1) {
                num++
            }
        }

        return num

    }

    var code = 1

    override fun createView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return inflater.inflate(R.layout.message_fragment_layout, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        initAdapter()

        messageRefreshView.setOnRefreshListener {

            messageList.clear()
            siXinList.clear()

            handler!!.postDelayed(Runnable {
                getMessage()
            }, 300)

        }

        val w = resources.getDimension(R.dimen.dp_10).toInt()
        setIndicator(messageTabLayout, w, w)

        messageTabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                val p = tab!!.position

                change(p)
            }

        })
    }


    private fun initTabLayout() {
        val view1 = messageTabLayout.getTabAt(0)!!.customView
        val view2 = messageTabLayout.getTabAt(1)!!.customView
        val text1 = view1!!.findViewById<TextView>(R.id.tvTabItemTitle)
        val text2 = view2!!.findViewById<TextView>(R.id.tvTabItemTitle)

        text1.text = "消息"
        text2.text = "私信"

        if (indexTabP == 0) {
            messageTabLayout.getTabAt(0)!!.select()
            text1.setTextColor(resources.getColor(R.color.red))
            text2.setTextColor(resources.getColor(R.color.black))
        } else {
            messageTabLayout.getTabAt(1)!!.select()
            text1.setTextColor(resources.getColor(R.color.black))
            text2.setTextColor(resources.getColor(R.color.red))
        }

    }

    var indexTabP = 0

    override fun onResume() {
        super.onResume()

        initTabLayout()

        messageList.clear()
        siXinList.clear()

        getMessage()

    }

    var isVisibleCode = false

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        this.isVisibleCode = isVisibleToUser
        super.setUserVisibleHint(isVisibleToUser)
    }

    var page = 1
    var keyWord = ""


    private fun getMessage() {
        send!!.getMessage(page, keyWord, handler)
        showLoadDialog(activity!!)
    }


    lateinit var adapter: MessageAdapter
    val list = arrayListOf<MessageInfo>()

    private fun initAdapter() {

        messageRecyclerView.layoutManager = LinearLayoutManager(activity)
        messageRecyclerView.addItemDecoration(DividerItemDecoration(activity, DividerItemDecoration.VERTICAL))

        adapter = MessageAdapter(activity!!, list)

        messageRecyclerView.adapter = adapter

        adapter.itemClickListener = object : ItemClickListener {
            override fun onItemClick(position: Int) {

                val intent = Intent(activity!!, MessageDetailsActivity::class.java)
                intent.putExtra("messageId", list[position])
                startActivityForResult(intent, 1234)

            }

            override fun onLongClick(position: Int) {

            }

        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1234 && resultCode == RESULT_OK) {

        }
    }
}